/***************************************************************************
 *   Salatinoid - Islamic Prayer Time Plasmoid                             *
 *   Copyright (C) 2009 by Tarek Saidi  <tarek.saidi@arcor.de>             *
 *                                                                         *
 *   Bismillah ar-Rahman ar-Rahim;                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program. If not, see <http://www.gnu.org/licenses/>.  *
 ***************************************************************************/

#include <QPen>
#include <QPainter>
#include <QFontMetrics>
#include <QWidget>
#include "graphicstextwidget.h"


GraphicsTextWidget::GraphicsTextWidget(const QString& text){
    setText(text);
}

void GraphicsTextWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->setRenderHint(QPainter::SmoothPixmapTransform);
    painter->setRenderHint(QPainter::Antialiasing);
    painter->setPen(Qt::white);
    painter->setFont(font());
    painter->drawText(contentsRect(), m_TextAlign, m_Text);
}

void GraphicsTextWidget::setText(const QString& text){
    m_Text = text;
    updateGeometry();
}

void GraphicsTextWidget::setFont(const QFont& font){
    QGraphicsWidget::setFont(font);
    updateGeometry();
}

void GraphicsTextWidget::setTextAlign(Qt::Alignment align){
    m_TextAlign = align;
}

void GraphicsTextWidget::updateGeometry(){
    QFontMetrics metric(font());
    textSize = metric.size(Qt::TextSingleLine,m_Text);
    QGraphicsLayoutItem::updateGeometry();
}

QSizeF GraphicsTextWidget::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const {
    if(which == Qt::MaximumSize){
        return QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
    }
    return textSize;
}
